package cn.est.controller;
import cn.est.pojo.Store;
import cn.est.constants.Constants;
import cn.est.dto.Page;
import cn.est.dto.Result;
import cn.est.service.StoreService;
import cn.est.utils.EmptyUtils;
import cn.est.utils.ResultUtils;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.Map;

@Controller
@RequestMapping("/client/store")
public class StoreController {

    @Resource
    private StoreService storeService;

    @RequestMapping("/search")
    @ResponseBody
    public Page<Store> searchStore(@RequestParam Map<String,Object> param) {
        Page<Store> storePage = null;
        try {
            Integer pageNo= EmptyUtils.isEmpty(param.get("pageNo"))? Page.DEFAULT_PAGE_NO:Integer.parseInt((String) param.get("pageNo"));
            Integer pageSize= EmptyUtils.isEmpty(param.get("pageSize"))? Page.DEFAULT_PAGE_SIZE:Integer.parseInt((String) param.get("pageSize"));
            storePage  = storeService.queryStorePageByMap(param, pageNo, pageSize);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return storePage;
    }

    @RequestMapping("/saveOrUpdateStore")
    @ResponseBody
    public Result saveOrUpdateStore(Store store){
        Integer flag=0;
        try {
            if(EmptyUtils.isEmpty(store.getId())){
                flag=storeService.qdtxAddStore(store);
            }else{
                flag=storeService.qdtxModifyStore(store);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag>=1?ResultUtils.returnSuccess():ResultUtils.returnFail();
    }

    @RequestMapping("/getStoreById")
    @ResponseBody
    public Result getStoreById(Long id){
        Result result= ResultUtils.returnSuccess();
        Store store=null;
        try {
            store=storeService.getStoreById(id);
            result.setData(store);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    @RequestMapping("/batchRemoveStore")
    @ResponseBody
    public Result batchRemoveStore(String ids){
        Integer flag=0;
        try {
            flag=storeService.qdtxBatchDeleteStore(ids);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResultUtils.returnSuccess();
    }

    @RequestMapping("/removeStoreById")
    @ResponseBody
    public Result removeStoreById(Long id){
        Integer flag=0;
        try {
            flag=storeService.qdtxDeleteStoreById(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResultUtils.returnSuccess();
    }
}
